<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button type="primary" @click="handleOrderDetail(selectedRow)" :disabled="!hasSelectedRows"> 查看订单 </a-button>
        <a-button type="primary" @click="handlerOptLog" :disabled="!hasSelectedRows" v-auth="'ACTION_gatheringRecordLog'"> 操作日志 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <OrderDetailModal @register="registerOrderDetailModal"></OrderDetailModal>
    <OptLogModal @register="registerOptLogModal"></OptLogModal>
  </div>
</template>
<script lang="ts" setup name="gatheringRecord">
import { ActionItem, TableAction, BasicTable } from '@apps/antd-admin/src/components/Table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { Api, checkSub } from './api';
import { columns, searchFormSchema } from './data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import OrderDetailModal from '../order/OrderDetailModal.vue';
import OptLogModal from '../optlog/OptLogModal.vue';
import { useModal } from '@apps/antd-admin/src/components/modal';

const { createMessage, createConfirm } = useMessage();
const [registerOrderDetailModal, { openModal: openOrderDetailModal }] = useModal();
const [registerOptLogModal, { openModal: openOptLogModal }] = useModal();

const { prefixCls, tableContext } = useBaseTable({
  tableProps: {
    title: '收款确认记录列表',
    listUrl: Api.page,
    delUrl: Api.remove,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: true,
    clickToRowSelect: true,
    rowKey: 'gatheringRecordId',
    rowSelection: {
      type: 'radio',
    },
    //操作列配置
    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});
//注册table数据
const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

function handleOrderDetail(record) {
  openOrderDetailModal(true, {
    record,
    isUpdate: false,
  });
}

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '财务确认',
      onClick: handleConfirm.bind(null, record),
      auth: 'ACTION_gatheringRecordConfirm',
    },
    {
      label: '财务冻结',
      onClick: handleFreeze.bind(null, record),
      auth: 'ACTION_gatheringRecordFreeze',
    },
  ];
}

function handleConfirm(record) {
  let gatheringRecordId = record.gatheringRecordId;
  console.info('gatheringRecordId', gatheringRecordId);
  let orderNo = record.orderNo;
  console.info('orderNo', orderNo);
  createConfirm({
    iconType: 'warning',
    title: '财务确认',
    content: '确认已收到订单号为' + orderNo + '的相关款项？',
    onOk: async () => {
      await checkSub({ gatheringRecordId: gatheringRecordId, isCheck: 3 });
      handleSuccess();
    },
    onCancel: async () => {},
  });
}

function handleFreeze(record) {
  let gatheringRecordId = record.gatheringRecordId;
  console.info('gatheringRecordId', gatheringRecordId);
  let orderNo = record.orderNo;
  console.info('orderNo', orderNo);
  createConfirm({
    iconType: 'warning',
    title: '财务冻结',
    content: '确认要冻结订单号为' + orderNo + '收款记录嘛？',
    onOk: async () => {
      await checkSub({ gatheringRecordId: gatheringRecordId, isCheck: 4 });
      handleSuccess();
    },
    onCancel: async () => {},
  });
}

function handlerOptLog() {
  let data = { busType: 'BusGatheringRecord', businessId: selectedRow.value.gatheringRecordId };
  openOptLogModal(true, {
    data,
    isUpdate: false,
  });
}

function handleSuccess() {
  reload();
}
</script>
